#!/bin/bash
# 定义需要管理的端口列表
ports=(9000 9001 9002 9003 9004)
# 关闭进程的函数
stop_process() {
  local port=$1
  pid=$(lsof -t -i:$port)
  if [ -n "$pid" ]; then
    echo "Stopping process on port $port with PID $pid"
    kill -15 $pid
    sleep 15  # 销毁资源
    echo "stop process on port $port"
  else
    echo "No process found on port $port"
  fi
}

# 启动进程的函数
start_process() {
  local port=$1
  echo "Starting process on port $port"
  nohup java -jar -Dloader.path=lib abcshop-api.jar --spring.profiles.active=pro --server.port=$port >/dev/null 2>&1 &
  sleep 15  # 等待进程启动
  echo "finishing process on port $port"
}

# 遍历端口列表，依次关闭并启动进程
for port in "${ports[@]}"; do
  stop_process $port
  start_process $port
done
echo "All processes have been restarted."